// components/card/card.js
var amapFile = require('../../utils/amap-wx');
var myAmapFun = new amapFile.AMapWX({
  key: '1cd3b5a1356e9cd779a3bf6208b12b63'
});
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'J3ZBZ-WWXKJ-57RFY-FR7UP-OMKPK-GMBSJ'
});
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    card: Object,
    moveX: Number,
  },

  /**
   * 组件的初始数据
   */
  data: {
    cards: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    touchMove(e) {
      var that = this;
      this.data.cards.delete = 0
      this.data.cards.collection = 0
      if (that.properties.moveX >= 0) {
        this.data.cards.collection = that.properties.moveX / 100
        this.data.cards.delete = 0
      } else {
        this.data.cards.delete = Math.abs(that.properties.moveX) / 100
        this.data.cards.collection = 0
      }
      that.setData({
        cards: that.data.cards
      })
    },
    touchEnd(e) {
      var that = this;
      // if(Math.abs(that.properties.moveX) >= 0){
      this.data.cards.delete = 0
      this.data.cards.collection = 0
      that.setData({
        cards: that.data.cards
      })
      // }
    },
    navTo(e) {
      console.log(e)
      var url = '../../pages/details/details?id=' + e.currentTarget.dataset.id + '&status=0'
      // var url = e.currentTarget.dataset.url
      wx.navigateTo({
        url: url
      })
    },
     mapImg(data) {
      var that = this;
      var height = '150';
      var width = '160';
      var size = width + "*" + height;
      myAmapFun.getStaticmap({
        zoom: 8,
        size: size,
        scale: 2,
        location:""+data.longitude+","+data.latitude+"",
        // markers: "-1,https://api.yipaiju.com/resources/images/wawa/postion.png,0:" + data.longitude + "," + data.latitude ,
        markers: "mid,0xFF0000,:" + data.longitude + "," + data.latitude ,
        labels: data.residential_quarter_name+',2,0,16,0xFFFFFF,0x008000:'+ data.longitude +','+ data.latitude +'',
        success: function (res) {
          console.log(data)
         data.mapImg = res.url
          that.setData({
            cards: data
          })
          // console.log(that.data.cards)
        },
        fail: function (info) {
          // wx.showModal({
          //   title: info.errMsg
          // })
        }
      })
    },
    nearby_search(data) { //地址解析附近地铁
      var that = this;
      // 调用接口
      let latitude = data.latitude;
      let longitude = data.longitude;
      qqmapsdk.search({
        keyword: '地铁', //搜索关键词
        location: latitude + ',' + longitude, //设置周边搜索中心点
        success: function (res) { //搜索成功后的回调
          if (res.data.length > 1) {
            data.metro = res.data[0];
            that.setData({
              cards: data
            })
            console.log(that.data.cards,'1213')
          }
        },
        fail: function (res) {
          console.log(res);
        },
        complete: function (res) {
          // console.log(res);
        }
      });
    },
  },

  lifetimes: {
    attached() {
      var that = this;
      that.mapImg(that.properties.card)
      that.nearby_search(that.properties.card)
      // console.log()
      that.setData({
        cards: that.properties.card
      })
    }
  },
})